import { Scene_MenuBase } from "@/scenes/Scene_MenuBase.js";
import { Window_ItemCategory } from "@/windows/Window_ItemCategory.js";
import { Window_ItemList } from "@/windows/Window_ItemList.js";
import { Window_MenuStatus } from "@/windows/Window_MenuStatus.js";

import { Game_Action } from "@/objects/GameAction.js";

import { SoundManager } from "@/managers/SoundManager.js";
import { config } from "@/managers/ConfigManager.js";
import { SceneManager } from "@/managers/SceneManager";

import { ItemController } from "@/controller/ItemController.js";


export class Scene_Item extends Scene_MenuBase {
    constructor() {
        super();
        this._action = null;
    }

    create() {
        super.create();
        this.createHelpWindow();
        this.createCategoryWindow();
        this.createItemWindow();
        this.createActorMenu();

        this.attachHandler();
    }

    createCategoryWindow() {
        this._categoryWindow = new Window_ItemCategory(config.width, 72);
        this._categoryWindow.y = this._helpWindow.height;
        this.addWindow(this._categoryWindow);
    }

    createItemWindow() {
        let wy = this._categoryWindow.y + this._categoryWindow.height;
        let wh = config.height - wy;
        this._itemWindow = new Window_ItemList(config.width, wh);
        this._itemWindow.y = wy;
        this._itemWindow.deactivate();
        this.addWindow(this._itemWindow);
    }

    createActorMenu() {
        this._actorWindow = new Window_MenuStatus(config.width - 240, config.height);
        this._actorWindow.x = 240;
        this._actorWindow.hide();
        this._actorWindow.deactivate();
        this.addChild(this._actorWindow);
    }

    createItemController(){
        this._itemController=new ItemController();
        this._itemController.on('item-use',(item,index)=>{
            if(item){
                if(!$gameParty.numItems(item)){
                    this._action?.clear();
                    this._itemWindow.refresh();
                }else{
                    this._itemWindow.refreshItem(index);
                }
            }
        });
    }

    attachHandler() {
        this._categoryWindow.setHandler('changed', category => {
            this._itemWindow.setCategory(category);
            this._itemWindow.deactivate();
        });
        this._categoryWindow.setHandler('ok', category => {
            this._itemWindow.setCategory(category);
            this._itemWindow.activate();
            this._itemWindow.select(0);
            this._categoryWindow.deactivate();
        });
        this._categoryWindow.setHandler('cancel', () => {
            this._categoryWindow.deactivate();
            SceneManager.pop();
        });


        this._itemWindow.setHandler('changed', (item) => {
            this._helpWindow.setText((item?.description) ?? '');
        });
        this._itemWindow.setHandler('ok', (item, index) => {
            if (!item) return;
            if (!this._action) {
                this._action = new Game_Action();
            } else {
                this._action.clear();
            }
            let action = this._action;
            action.setItemObject(item);
            if (action.isForFriend()) {
                if (action.isForOne()) {
                    this._itemController.setItem(item,index);
                    this._actorWindow.open();
                    this._actorWindow.activate();
                    this._itemWindow.deactivate();
                } else if (action.isForAll()) {
                    this.useItemForAll();
                }
            } else {
                $gameParty.consumeItem(item);
                action.applyGlobal();
                this.checkEvent();
                this._itemController.emit('item-use');
            }
        });
        this._itemWindow.setHandler('cancel', () => {
            this._itemWindow.deactivate();
            this._categoryWindow.activate();
        });

        this._actorWindow.setHandler('ok', (actor, index) => {
            if (!actor || !this._action?.isValid()) return;
            let action=this._action;

            action.setSubject(actor);
            actor.useItem(action.item());
            let times = action.numRepeats();
            for (let i = 0; i < times; ++i) {
                action.apply(actor);
            }
            action.applyGlobal();
            this.checkEvent();
            this._actorWindow.refreshItem(index);
            this._itemController.emit('item-use',actor,index);
        });
        this._actorWindow.setHandler('cancel', () => {
            this._actorWindow.deactivate();
            this._actorWindow.close();
            this._itemWindow.activate();
        });
    }

    useItemForAll() {
        if (!this._action?.isValid()) return;
        this.playSeForItem();
        let action = this._action;
        if (action.isForAll()) {
            let actor = this.user();
            this._action.setSubject(actor);
            actor.useItem(this._action.item());
            let times = action.numRepeats();
            for (let battler of $gameParty.members()) {
                for (let i = 0; i < times; ++i) {
                    action.apply(battler);
                }
            }
            action.applyGlobal();
            this.checkEvent();
            this._itemController.emit('item-use');
        }
    }

    user() {
        let members = $gameParty.movableMembers();
        let bestActor = members[0];
        let bestPha = 0;
        for (let i = 0; i < members.length; i++) {
            if (members[i].pha > bestPha) {
                bestPha = members[i].pha;
                bestActor = members[i];
            }
        }
        return bestActor;
    }

    playSeForItem() {
        SoundManager.playUseItem();
    }

    checkEvent(){
        // this.checkCommonEvent();
        // this.checkGameover();
    }
}